/**
 * 
 */
package edu.cse5311.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.cse5311.entity.DataBuffer;
import edu.cse5311.entity.DataDisk;
import edu.cse5311.entity.LogBuffer;
import edu.cse5311.entity.LogDisk;
import edu.cse5311.entity.Transaction;

/**
 * @author karthik MG / Shwetha Narayan
 * This class instantiates and then initializes all the tables, lists, maps used 
 * The data disk is initialized with items A-Z with its initial value zero
 * The free data buffer list contains all the buffers (0-19)
 * Enum is used in order to find the disk index of an item stored on disk
 */
public class Initialize {
	//Data disk
	public static List<DataDisk> dataDisk = null;
	//Main memory buffers
	public static List<DataBuffer> dataCache = null;
	
	//List containing the index of the free buffers of the data buffer
	public static List<Integer> freeDataBufferList = null;
	
	//Transaction table containing all the transaction which are current in the system.
	public static Map<Integer, Transaction> transactionTable = null;
	//Keeps track of the data present in the data buffer, and maps it to the data disk.
	public static Map<Integer, Integer> dataBufferAllocationTable = null;
	
	//Log buffer
	public static List<LogBuffer> logBufferList = null;
	//Log disk, simulating the Log File
	public static List<LogDisk> logDisk = null;
	
	//Enum for the Data disk
	DISKINDEX diskindex = null;
	
	public enum DISKINDEX 
	{
		A(0), B(1), C(2),D(3),E(4),F(5),G(6),H(7),I(8),J(9),K(10),L(11),M(12),N(13),O(14),P(15),Q(16),R(17),S(18),T(19),U(20),V(21),W(22),X(23),Y(24),Z(25);
		private int i;
		
		/**
		 * @return the i
		 */
		public int getI() {
			return i;
		}

		private DISKINDEX(int i)
		{
			this.i = i;
		}				
	};


	/**
	 * This function initializes all the objects, tables, lists, maps used
	 */
	void initialising() {
		DataDisk A = new DataDisk();
		A.setItemName("A");
		A.setItemValue(0);

		DataDisk B = new DataDisk();
		B.setItemName("B");
		B.setItemValue(0);

		DataDisk C = new DataDisk();
		C.setItemName("C");
		C.setItemValue(0);

		DataDisk D = new DataDisk();
		D.setItemName("D");
		D.setItemValue(0);

		DataDisk E = new DataDisk();
		E.setItemName("E");
		E.setItemValue(0);

		DataDisk F = new DataDisk();
		F.setItemName("F");
		F.setItemValue(0);

		DataDisk G = new DataDisk();
		G.setItemName("G");
		G.setItemValue(0);

		DataDisk H = new DataDisk();
		H.setItemName("H");
		H.setItemValue(0);

		DataDisk I = new DataDisk();
		I.setItemName("I");
		I.setItemValue(0);

		DataDisk J = new DataDisk();
		J.setItemName("J");
		J.setItemValue(0);

		DataDisk K = new DataDisk();
		K.setItemName("K");
		K.setItemValue(0);
		
		DataDisk L = new DataDisk();
		L.setItemName("L");
		L.setItemValue(0);
		
		DataDisk M = new DataDisk();
		M.setItemName("M");
		M.setItemValue(0);

		DataDisk N = new DataDisk();
		N.setItemName("N");
		N.setItemValue(0);

		DataDisk O = new DataDisk();
		O.setItemName("O");
		O.setItemValue(0);

		DataDisk P = new DataDisk();
		P.setItemName("P");
		P.setItemValue(0);

		DataDisk Q = new DataDisk();
		Q.setItemName("Q");
		Q.setItemValue(0);

		DataDisk R = new DataDisk();
		R.setItemName("R");
		R.setItemValue(0);

		DataDisk S = new DataDisk();
		S.setItemName("S");
		S.setItemValue(0);

		DataDisk T = new DataDisk();
		T.setItemName("T");
		T.setItemValue(0);

		DataDisk U = new DataDisk();
		U.setItemName("U");
		U.setItemValue(0);

		DataDisk V = new DataDisk();
		V.setItemName("V");
		V.setItemValue(0);

		DataDisk W = new DataDisk();
		W.setItemName("W");
		W.setItemValue(0);

		DataDisk X = new DataDisk();
		X.setItemName("X");
		X.setItemValue(0);

		DataDisk Y = new DataDisk();
		Y.setItemName("Y");
		Y.setItemValue(0);

		DataDisk Z = new DataDisk();
		Z.setItemName("Z");
		Z.setItemValue(0);

		dataDisk = new ArrayList<DataDisk>();
		dataDisk.add(A);
		dataDisk.add(B);
		dataDisk.add(C);
		dataDisk.add(D);
		dataDisk.add(E);
		dataDisk.add(F);
		dataDisk.add(G);
		dataDisk.add(H);
		dataDisk.add(I);
		dataDisk.add(J);
		dataDisk.add(K);
		dataDisk.add(L);
		dataDisk.add(M);
		dataDisk.add(N);
		dataDisk.add(O);
		dataDisk.add(P);
		dataDisk.add(Q);
		dataDisk.add(R);
		dataDisk.add(S);
		dataDisk.add(T);
		dataDisk.add(U);
		dataDisk.add(V);
		dataDisk.add(W);
		dataDisk.add(X);
		dataDisk.add(Y);
		dataDisk.add(Z);

		// Initialize all other data structures that represent various disks,
		// main memory buffers, log files and log buffers
		dataCache = new ArrayList<DataBuffer>();
		logBufferList = new ArrayList<LogBuffer>();
		logDisk = new ArrayList<LogDisk>();
		
		//List of free data buffers
		freeDataBufferList = new ArrayList<Integer>();
		//There are 20 free buffers before any of the transaction start.
		for(int i = 0; i <20 ; i++)
			freeDataBufferList.add(i);
		
		transactionTable = new HashMap<Integer, Transaction>();
		dataBufferAllocationTable = new HashMap<Integer, Integer>();
	}
}
